package cn.edu.xmu.oomall.liquidation.model.bo.liquid.revenue;

import cn.edu.xmu.oomall.liquidation.dao.LiquidationDao;
import cn.edu.xmu.oomall.liquidation.microservice.OrderService;
import cn.edu.xmu.oomall.liquidation.microservice.vo.OrderItemRetVo;
import cn.edu.xmu.oomall.liquidation.microservice.vo.PaymentRetVo;
import cn.edu.xmu.oomall.liquidation.microservice.vo.SimpleOrderRetVo;
import cn.edu.xmu.oomall.liquidation.model.bo.Liquidation;
import cn.edu.xmu.oomall.liquidation.model.bo.RevenueItem;
import cn.edu.xmu.oomall.liquidation.model.bo.liquid.calc.CalcTotal;
import cn.edu.xmu.oomall.liquidation.model.bo.liquid.calc.LiquidationCalc;
import cn.edu.xmu.oomall.liquidation.model.bo.liquid.calc.PaymentCalcInfo;
import cn.edu.xmu.privilegegateway.annotation.util.InternalReturnObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
public class OrderPaymentLiquid extends BasePaymentLiquid {
    @Autowired
    private OrderService orderService;

    @Autowired
    private LiquidationCalc liquidationCalc;

    @Override
    protected CalcTotal liquidateSinglePayment(PaymentRetVo payment, LocalDateTime liquidDateTime) {
        CalcTotal platformCalcTotal = new CalcTotal(liquidUtil.getPlatformCalcFieldList());
        // 获取父订单Id
        SimpleOrderRetVo parentOrder = orderService.getOrderBySn(payment.getDocumentId()).getData();
        Long parentOrderId = parentOrder.getId();

        // 用父订单Id获得所有的子订单
        InternalReturnObject orderByPid = orderService.getOrderByPid(parentOrderId);
        List<SimpleOrderRetVo> childOrder = (List<SimpleOrderRetVo>) orderByPid.getData();

        for (SimpleOrderRetVo order : childOrder) {
            // 生成清算单统计类
            CalcTotal calcTotal = new CalcTotal(liquidUtil.getShopCalcFieldList());
            Long shopId = order.getShopId();
            Liquidation liquid = liquidUtil.getLiquid(shopId, liquidDateTime);
            Long liquidId = liquid.getId();

            // 清算快递费
            CalcTotal expressFeeCalcTotal = liquidateExpressFee(order, liquidId, payment);
            calcTotal.add(expressFeeCalcTotal);

            // 清算子订单的订单明细
            CalcTotal childOrderCalcTotal = liquidateChildOrder(order, liquidId, payment);
            calcTotal.add(childOrderCalcTotal);

            //更新清算表
            //Todo 清算表的并发问题不知道要不要处理
            calcTotal.changeLiquid(liquid);
            liquidUtil.updateLiquid(liquid);

            // 更新平台统计信息
            platformCalcTotal.add(calcTotal);
        }
        return platformCalcTotal;
    }

    private CalcTotal liquidateExpressFee(SimpleOrderRetVo order, Long liquidId, PaymentRetVo payment) {
        CalcTotal calcTotal = new CalcTotal();
        Long expressFee = order.getExpressFee();

        RevenueItem revenueItem = new RevenueItem();
        revenueItem.setPaymentId(payment.getId());
        revenueItem.setShopId(order.getShopId());
        revenueItem.setOrderId(order.getId());
        revenueItem.setOrderitemId(0L);
        revenueItem.setLiquidId(liquidId);
        revenueItem.setExpressFee(expressFee);
        liquidUtil.insertRevenue(revenueItem);
        calcTotal.add("expressFee", expressFee);

        return calcTotal;
    }

    private CalcTotal liquidateChildOrder(SimpleOrderRetVo order, Long liquidId, PaymentRetVo payment) {
        CalcTotal calcTotal = new CalcTotal(liquidUtil.getShopCalcFieldList());
        // foreach清算订单明细 1个订单明细生成1个进账单
        List<OrderItemRetVo> orderItemList = order.getOrderItems();
        for (OrderItemRetVo orderItem : orderItemList) {
            // 初始化进账单
            RevenueItem revenueItem = new RevenueItem();
            revenueItem.setLiquidId(liquidId);
            revenueItem.setPaymentId(payment.getId());
            revenueItem.setShopId(order.getShopId());
            revenueItem.setOrderId(order.getId());
            revenueItem.setOrderitemId(orderItem.getId());
            revenueItem.setProductId(orderItem.getProductId());
            revenueItem.setProductName(liquidUtil.getProductNameById(orderItem.getProductId()));
            Long amount = (orderItem.getDiscountPrice() + orderItem.getPoint()) * orderItem.getQuantity() / 10;
            revenueItem.setAmount(amount);
            revenueItem.setQuantity(orderItem.getQuantity());
            revenueItem.setShopRevenue(amount);

            // 计算杂项
            PaymentCalcInfo calcInfo = new PaymentCalcInfo(orderItem.getOnsaleId(), order.getCustomerId(),
                    orderItem.getProductId(), orderItem.getQuantity(), orderItem.getId(), revenueItem);
            Map<String, Long> calcMap = liquidationCalc.calc(calcInfo);
            // 插入数据库
            liquidUtil.insertRevenue(revenueItem);
            //更新统计信息
            calcMap.put("shopRevenue", revenueItem.getShopRevenue());
            calcTotal.add(calcMap);
        }
        return calcTotal;
    }
}
